package jst.householdmanager.activities;

import java.util.Date;

import jst.householdmanager.R;
import jst.householdmanager.database.ItemsDataSource;
import jst.householdmanager.database.PurchaseListPosDataSource;
import jst.householdmanager.database.table.Items;
import jst.householdmanager.database.table.PurchaseListPos;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditPurchaseListPosAct extends Activity {
	public enum EditPurchaseListAction {
		ADD_PURCHASE, PROCESS_PURCHASE;
	}

	private int selectedItemId = -1;
	private Date selectedFinDate;
	private EditPurchaseListAction actionId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_edit_purchaselistpos);

		TextView txt_item_name = ((TextView) findViewById(R.id.edit_purlistpos_item_name));
		TextView txt_item_desc = ((TextView) findViewById(R.id.edit_purlistpos_item_desc));
		EditText edt_desc = ((EditText) findViewById(R.id.edit_purlistpos_edt_desc));
		EditText edt_amount = ((EditText) findViewById(R.id.edit_purlistpos_edt_amount));
		Button btn_amount_p = ((Button) findViewById(R.id.edit_purlistpos_btn_amount_p));
		Button btn_amount_n = ((Button) findViewById(R.id.edit_purlistpos_btn_amount_n));
		EditText edt_finish = ((EditText) findViewById(R.id.edit_purlistpos_edt_finish));
		Button btn_finish_p = ((Button) findViewById(R.id.edit_purlistpos_btn_finish_p));
		Button btn_finish_n = ((Button) findViewById(R.id.edit_purlistpos_btn_finish_n));

		Button btn_save = ((Button) findViewById(R.id.edit_purlistpos_btn_save));

		Bundle givenData = getIntent().getExtras();

		if (givenData != null) {
			this.selectedItemId = givenData.getInt("ItemId");
			this.selectedFinDate = (Date) givenData.get("FinDate");
			this.actionId = (EditPurchaseListAction) givenData.get("ActionId");

			Items selectedItem = ItemsDataSource.read(this.selectedItemId);
			if (selectedItem == null) {
				Log.e("JST-Activity", "Given ItemID: " + this.selectedItemId + " not found! " + EditPurchaseListPosAct.class);
			}

			txt_item_name.setText(selectedItem.getName());
			txt_item_desc.setText(selectedItem.getDescription());

			if ((this.selectedFinDate != null) || (this.actionId != EditPurchaseListAction.ADD_PURCHASE)) {
				PurchaseListPos selectedPurListPos = PurchaseListPosDataSource.read(this.selectedItemId, this.selectedFinDate);
				if (selectedPurListPos != null) {
					edt_desc.setText(selectedPurListPos.getComment());
					edt_amount.setText(Integer.toString(selectedPurListPos
							.getAmount()));
					edt_finish.setText(Integer.toString(selectedPurListPos
							.getAmount()));
				}

				edt_finish.setVisibility(0);
				btn_finish_n.setVisibility(0);
				btn_finish_p.setVisibility(0);

				edt_desc.setEnabled(false);

				edt_amount.setEnabled(false);
				btn_amount_n.setEnabled(false);
				btn_amount_p.setEnabled(false);

			} else {
				edt_amount.setText("1");
			}

		} else {
			Log.e("JST-Activity", "No data given to " + EditPurchaseListPosAct.class);
		}

		btn_amount_p.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText edt_amount = ((EditText) findViewById(R.id.edit_purlistpos_edt_amount));
				Integer value;
				try {
					value = Integer.parseInt(edt_amount.getText().toString());
					value++;
				} catch (NumberFormatException e) {
					value = 1;
				}
				edt_amount.setText(value.toString());
			}
		});

		btn_amount_n.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText edt_amount = ((EditText) findViewById(R.id.edit_purlistpos_edt_amount));
				Integer value;
				try {
					value = Integer.parseInt(edt_amount.getText().toString());
					if (value > 1) {
						value--;
					}
				} catch (NumberFormatException e) {
					value = 1;
				}
				edt_amount.setText(value.toString());
			}
		});

		btn_finish_p.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText edt_finish = ((EditText) findViewById(R.id.edit_purlistpos_edt_finish));
				Integer value;
				try {
					value = Integer.parseInt(edt_finish.getText().toString());
					value++;
				} catch (NumberFormatException e) {
					value = 1;
				}
				edt_finish.setText(value.toString());
			}
		});

		btn_finish_n.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText edt_finish = ((EditText) findViewById(R.id.edit_purlistpos_edt_finish));
				Integer value;
				try {
					value = Integer.parseInt(edt_finish.getText().toString());
					if (value > 1) {
						value--;
					}
				} catch (NumberFormatException e) {
					value = 1;
				}
				edt_finish.setText(value.toString());
			}
		});

		btn_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditPurchaseListPosAct.this.saveChanges();
			}
		});

		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.edit_action, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;
		case R.id.mnu_save:
			this.saveChanges();
			break;
		case R.id.mnu_delete:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(getText(R.string.are_you_sure));
			builder.setMessage(getText(R.string.delete_item));
			// yes
			builder.setPositiveButton(getText(R.string.yes), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// delete item
					PurchaseListPosDataSource.delete(new PurchaseListPos(EditPurchaseListPosAct.this.selectedItemId,
							EditPurchaseListPosAct.this.selectedFinDate));
					dialog.dismiss();
					Intent intent = new Intent();
					intent.putExtra("dataChanged", true);
					setResult(RESULT_OK, intent);
					finish();
				}
			});
			// no
			builder.setNegativeButton(getText(R.string.no), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	private void saveChanges() {
		EditText edt_desc = ((EditText) findViewById(R.id.edit_purlistpos_edt_desc));
		// check amount
		Integer amount = 0;

		if (this.actionId == EditPurchaseListAction.PROCESS_PURCHASE) {
			this.selectedFinDate = new Date();
			EditText edt_finish = ((EditText) findViewById(R.id.edit_purlistpos_edt_finish));
			try {
				amount = Integer.parseInt(edt_finish.getText().toString());
			} catch (NumberFormatException e) {
				amount = 0;
			}
			if (amount == 0) {
				Toast.makeText(this, R.string.required_numeric, Toast.LENGTH_LONG).show();
				return;
			}
			PurchaseListPosDataSource.delete(new PurchaseListPos(
					this.selectedItemId, null));

		} else if (this.actionId == EditPurchaseListAction.ADD_PURCHASE) {
			EditText edt_amount = ((EditText) findViewById(R.id.edit_purlistpos_edt_amount));
			try {
				amount = Integer.parseInt(edt_amount.getText().toString());
			} catch (NumberFormatException e) {
				amount = 0;
			}
			if (amount == 0) {
				Toast.makeText(this, R.string.required_numeric, Toast.LENGTH_LONG).show();
				return;
			}
		}
		
		String descText = edt_desc.getText().toString();
		PurchaseListPos oldPurchaseListPos = PurchaseListPosDataSource.read(this.selectedItemId, this.selectedFinDate);
		if(oldPurchaseListPos != null) {
			amount += oldPurchaseListPos.getAmount();
			if(descText.matches("")) {
				descText = oldPurchaseListPos.getComment();
			}
		}
		
		PurchaseListPosDataSource.save(new PurchaseListPos(this.selectedItemId,
				this.selectedFinDate, amount, descText));
		// return data changed flag
		Intent intent = new Intent();
		intent.putExtra("dataChanged", true);
		setResult(RESULT_OK, intent);
		finish();
	}

}
